/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.ShowServlet;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Model.ObjectBean.ViewBean.*;
import java.io.PrintWriter;
import Controller.CoreServlet.Ultility.SSCookie_Login;
import Model.BeanUltility.ResultsetMapper;
import Model.DataUltility.DatabaseConnector;
import java.sql.ResultSet;
import java.util.ArrayList;
import Model.DataUltility.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import Model.BeanUltility.*;
import java.lang.reflect.InvocationTargetException;
import Controller.CoreServlet.Ultility.ShoppingCart;
import com.sun.faces.application.resource.ResourceManager;
/**
 *
 * @author Tran Nguyen Anh Tu
 */
public class WelcomeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
       
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0. backward compatibility
        
        // Lam cho proxy caches bit day la mot old page
        response.setDateHeader("Expires", -1); // Proxies.
        String orderstate=request.getParameter("order");
        if(orderstate==null)
            orderstate="notclear";        
        if(orderstate.equals("clear"))
        {
            ShoppingCart cart=(ShoppingCart) request.getSession().getAttribute("shoppingcart");
            cart.getItemsOrdered().clear();
        }
     
    
          
        SSCookie_Login.CheckandRecover(request);
         //INIT BEAN
        ArrayList listTopSeller;
        listTopSeller=ResourceMapper.getResourceBestSeller();
        
        ArrayList listDiscount;
        listDiscount=ResourceMapper.getResourceDiscount();
        
        ArrayList listNewItems;
        listNewItems=ResourceMapper.getResourceNew();
              
        request.getSession().setAttribute("listDiscount", listDiscount);
        request.getSession().setAttribute("listNewItems", listNewItems);
        request.getSession().setAttribute("listTopSeller", listTopSeller);

        
        request.getSession().setAttribute("listDiscount", listDiscount);
        request.getSession().setAttribute("listNewItems", listNewItems);
        request.getSession().setAttribute("listTopSeller", listTopSeller);
        
         RequestDispatcher dispatcher= request.getRequestDispatcher("/MainPages/home.jsp");
         dispatcher.forward(request, response);
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    } 
}
